package com.sxtyxt.widget;

import com.sxtyxt.online.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 自定义titlebar
 */
public class MyCustomTitleBar extends LinearLayout {

    private ImageView btnLeft;
    private TextView btnRight;
    private TextView txtTitle;
    private LinearLayout titleBar;

    public MyCustomTitleBar(Context context) {
        super(context);
    }

    public MyCustomTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.comm_titlebar, this);

        btnLeft = (ImageView) findViewById(R.id.bar_btn_left);
        btnRight = (TextView) findViewById(R.id.bar_txt_right);

        txtTitle = (TextView) findViewById(R.id.bar_title);
        titleBar = (LinearLayout) findViewById(R.id.titleBar);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyCustomTitleBar);
        try {
            // 设置title
            String title = typedArray
                    .getString(R.styleable.MyCustomTitleBar_barTitle);
            if (title != null) {
                txtTitle.setText(title);
            }

            boolean isShowTitle = typedArray.getBoolean(
                    R.styleable.MyCustomTitleBar_isShowTitle, true);
            if (isShowTitle) {
                txtTitle.setVisibility(View.VISIBLE);
            } else {
                txtTitle.setVisibility(View.INVISIBLE);
            }

            // 设置左侧按钮背景图片
            Drawable leftDrawable = typedArray
                    .getDrawable(R.styleable.MyCustomTitleBar_leftBtnDrawable);
            if (leftDrawable != null) {
                btnLeft.setImageDrawable(leftDrawable);
            }
            // 设置按钮text
            String lbtnText = typedArray
                    .getString(R.styleable.MyCustomTitleBar_leftBtnText);
            if (lbtnText != null) {
                //btnLeft.setText(lbtnText);
            }

            boolean isShowLeft = typedArray.getBoolean(
                    R.styleable.MyCustomTitleBar_isShowLeft, true);
            if (isShowLeft) {
                btnLeft.setVisibility(View.VISIBLE);
            } else {
                btnLeft.setVisibility(View.INVISIBLE);
            }

            // 设置右侧按钮背景图片
            Drawable rightDrawable = typedArray
                    .getDrawable(R.styleable.MyCustomTitleBar_rightBtnDrawable);

            // 设置按钮text
            String rbtnText = typedArray
                    .getString(R.styleable.MyCustomTitleBar_rightBtnText);
            if (rbtnText != null) {
                btnRight.setText(rbtnText);
            }

            boolean isShowRight = typedArray.getBoolean(
                    R.styleable.MyCustomTitleBar_isShowRight, false);
            if (isShowRight) {
                btnRight.setVisibility(View.VISIBLE);
            } else {
                btnRight.setVisibility(View.INVISIBLE);
            }

            // 设置title的背景图片
            Drawable titleBarDrawable = typedArray
                    .getDrawable(R.styleable.MyCustomTitleBar_barBackground);

            if (titleBarDrawable != null) {
                titleBar.setBackgroundDrawable(titleBarDrawable);
            }
        } catch (Exception e) {

        } finally {
            typedArray.recycle();
        }
    }

    /**
     * 设置title
     *
     * @param title
     */
    public void mSetTitle(String title) {
        this.txtTitle.setText(title);
        this.txtTitle.setVisibility(View.VISIBLE);
    }

    /**
     * 根据resId设置title
     *
     * @param resId
     */
    public void mSetTitle(int resId) {
        this.txtTitle.setText(resId);
        this.txtTitle.setVisibility(View.VISIBLE);
    }

    /**
     * 获取btnLeft
     *
     * @return
     */
    public ImageView mGetBtnLeft() {
        return btnLeft;
    }

    /**
     * 获取btnRight
     *
     * @return
     */
    public TextView mGetBtnRight() {
        return btnRight;
    }

    /**
     * 设置右侧按钮监听器
     *
     * @param onRightClickListener
     */
    public void mSetOnRightButtonClickListener(
            OnClickListener onRightClickListener) {
        btnRight.setOnClickListener(onRightClickListener);
    }

    /**
     * 设置左侧按钮监听器
     *
     * @param onLeftClickListener
     */
    public void mSetOnLeftButtonClickListener(
            OnClickListener onLeftClickListener) {
        btnLeft.setOnClickListener(onLeftClickListener);
    }
}
